package org.me.marble;

import android.view.*;
import android.widget.*;
import android.content.*;

import android.net.Uri;

/**
 *
 * @author tungi
 */

public class MainOptionsMenu implements AdapterView.OnItemClickListener,
            DialogInterface.OnClickListener{
    private OptionsActivity _options;
    private String dialog_type="";

    public MainOptionsMenu(OptionsActivity _options){
        this._options=_options;
    }

    public void createView(){
        //set the current content view of the activity
        _options.setContentView(R.layout.optionsmain);

        ListView view;
        ArrayAdapter<String> adapter;

        //set first litview -> first section
        adapter=new ArrayAdapter<String>(_options,
                android.R.layout.simple_list_item_multiple_choice,
                new String[]{"Show FPS","Enable Sound","Enable Vibrate"});
        view=(ListView)_options.findViewById(R.list.fpssoundvibrate);
        view.setAdapter(adapter);
        view.setId(R.list.fpssoundvibrate);
        view.setOnItemClickListener(this);
        view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        view.setItemChecked(0,OptionsLibrary.fps);
        view.setItemChecked(1,OptionsLibrary.sound);
        view.setItemChecked(2,OptionsLibrary.vibrate);

        //set second listview -> first section
        adapter=new IconArrayAdapter<String>(_options,
                R.layout.iconlistitem,R.iconlistitem.icon,
                R.iconlistitem.label,
                new int[]{android.R.drawable.ic_menu_more,
                     android.R.drawable.ic_menu_more},
                new String[]{"Graphics Options","Controls Settings"},
                new boolean[]{true,true});

        view=(ListView)_options.findViewById(R.list.settings);
        view.setAdapter(adapter);
        view.setId(R.list.settings);
        view.setOnItemClickListener(this);

        //set third listview -> second section
        adapter=new ArrayAdapter<String>(_options,
                android.R.layout.simple_list_item_1,
                new String[]{"Erase Saved Game","Restore Default Settings"});
        view=(ListView)_options.findViewById(R.list.data);
        view.setAdapter(adapter);
        view.setId(R.list.data);
        view.setOnItemClickListener(this);

        //set fourth  -> third section
        adapter=new ArrayAdapter<String>(_options,
                android.R.layout.simple_list_item_1,
                new String[]{"Information","Go To Web Site"});
        view=(ListView)_options.findViewById(R.list.about);
        view.setAdapter(adapter);
        view.setId(R.list.about);
        view.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> arg0, View view, int pos, long id){
        if(arg0.getId()==R.list.fpssoundvibrate){
            if(pos==0){
                OptionsLibrary.fps=!OptionsLibrary.fps;
                view.setSelected(OptionsLibrary.fps);
            }
            else if(pos==1){
                if(OptionsLibrary.sound)
            		SoundManager.stopSound(sounds.Main.ordinal());
                else
                	SoundManager.playSound(sounds.Main.ordinal(),-1,1.0f);
                OptionsLibrary.sound=!OptionsLibrary.sound;
                view.setSelected(OptionsLibrary.sound);
            }
            else if(pos==2){
                OptionsLibrary.vibrate=!OptionsLibrary.vibrate;
                view.setSelected(OptionsLibrary.vibrate);
            }
        }
        else if(arg0.getId()==R.list.settings){
            switch(pos){
                case 0: {
                    //graphics options -> show to next view
                    _options.changeView(R.layout.graphicsoptions);
                }break;
                case 1: {
                    //control options -> show to next view
                    _options.changeView(R.layout.controlsettings);
                }break;
                default: break;
            }
        }
        else if(arg0.getId()==R.list.data){
            switch(pos){
                case 0: {
                    new android.app.AlertDialog.Builder(_options)
                        .setView(_options.getAlertDialogView(
                                "Are you sure to "+'\n'
                                   +"delete the current game state?"))
                        .setPositiveButton("Yes",this)
                        .setNegativeButton("No",this)
                        .show();
                    dialog_type="data";
                    }break;
                case 1:{
                    new android.app.AlertDialog.Builder(_options)
                        .setView(_options.getAlertDialogView(
                                    "Are you sure to "+'\n'
                                   +"restore default settings?"))
                        .setPositiveButton("Yes",this)
                        .setNegativeButton("No",this)
                        .show();
                    dialog_type="settings";
                }break;
                default: break;
            }
        }
        else if(arg0.getId()==R.list.about){
            switch(pos){
                case 0: {
                    //show informations
                    _options.changeView(R.layout.informationmenu);
                }break;
                case 1: {
                    //go to website
                    String url="http://code.google.com/p/marblegame/";
                    Intent browser=new Intent(Intent.ACTION_VIEW);
                    browser.setData(Uri.parse(url));
                    _options.startActivity(browser);
                }break;
                default: break;
            }
        }
    }

    public void onClick(DialogInterface arg0,int arg1){
        if(arg1==DialogInterface.BUTTON_POSITIVE){
            arg0.dismiss();
            if(dialog_type.equals("data")){
                //erase saved game -> set everything to default
            	for(int i=1;i<=OptionsLibrary.reached_level;++i){
                	SharedPreferences.Editor ed=_options.getSharedPreferences(
                			Context.ACTIVITY_SERVICE,Context.MODE_PRIVATE).edit();
                	
        			ed.putInt("level"+Integer.toString(i)+"_normal",0);
        			ed.putInt("level"+Integer.toString(i)+"_point",0);
        			ed.putFloat("level"+Integer.toString(i)+"_time",0.0f);
        			
                	ed.commit();
            	}
                OptionsLibrary.eraseSaves();
            }
            else if(dialog_type.equals("settings")){
                OptionsLibrary.restoreDefaultSettings();
                
                ListView view=(ListView)_options.findViewById(R.list.fpssoundvibrate);
        
                view.setItemChecked(0,OptionsLibrary.fps);
                view.setItemChecked(1,OptionsLibrary.sound);
                view.setItemChecked(2,OptionsLibrary.vibrate);
            }
            dialog_type="";
        }
        else if(arg1==DialogInterface.BUTTON_NEGATIVE){
            arg0.dismiss();
        }
    }
}
